from PySide2.QtCore import Qt,QFile
from PySide2.QtXml import QDomDocument

class MyFileUtil:
    def __init__(self):
        pass

    @staticmethod
    def read2String(fileQrc, fileState, encoding, char=''):
        qtFile = QFile(fileQrc)
        str_file_content = '';
        if not qtFile.exists():
            return ''
        if not qtFile.open(fileState):
            return ''
        try:
            while not qtFile.atEnd():
                qtBytes = qtFile.readLine()  #返回QByteArray类型
                pyBytes = bytes(qtBytes.data())  #将QByteArray转换为bytes类型
                lineStr = pyBytes.decode(encoding)   #bytes转换为str类型
                lineStr = lineStr.strip()           #去除结尾增加的空行
                str_file_content += lineStr + char
        finally:
            qtFile.close()
        return str_file_content

class MyXmlUtil:
    @staticmethod
    def read2XmlDocDocument(fileQrc, fileState):
        appXml = QDomDocument()
        qtFile = QFile(fileQrc)
        if not qtFile.exists():
            return None
        if not qtFile.open(fileState):
            return None
        if not appXml.setContent(qtFile):
            qtFile.close()
            return None
        qtFile.close()
        return appXml